﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;
using Newtonsoft.Json;

namespace wjy自制小框架.Common
{
    /// <summary>
    /// 核心Sql查询
    /// wjy
    /// </summary>
    class Db
    {
        /// <summary>
        /// 查询Sql
        /// </summary>
        public static List<dynamic> Connect(string Sql)
        {
            List<dynamic> list = new List<dynamic>();

            // 连接字符串
            string connectionString = $"Data Source={ Application.StartupPath }\\database.db";

            try
            {
                using (var connection = new SQLiteConnection(connectionString))
                {
                    connection.Open();

                    // 执行查询
                    using (var command = new SQLiteCommand(Sql, connection))
                    {
                        using (var reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                var mode = new Dictionary<string, dynamic>();
                                for (int i = 0; i < reader.FieldCount; i++)
                                {
                                    mode.Add(reader.GetName(i), reader.GetValue(i));
                                }
                                list.Add(mode);
                            }
                        }
                    }
                }
            }
            catch { }

            return list;
        }
    }
}
